<?php
if(isset($_GET['url'])) {
  $fullUrl = $_GET['url'];
  if(isset($_GET['passkey'])){
    $fullUrl = $_GET['url']."&passkey=".$_GET['passkey'];
  }
} else {
  die('Error: downhash parameter not specified.');
}
$data = getCurl($fullUrl);
// Decode bencoded data
$decoded = bdecode($data);

// Extract info dictionary
$info = $decoded['info'];

// Sort keys in info dictionary
ksort($info);

// Encode info dictionary again
$encoded = bencode($info);

// Calculate SHA1 hash of encoded info dictionary
$infoHash = sha1($encoded, true);

// Output info hash as hex string
$infoHashHex = bin2hex($infoHash);
echo  $infoHashHex;

// Bencode decoding function
function bdecode($data, &$pos = 0) {
    if ($data[$pos] == 'i') {
        $pos++;
        $end = strpos($data, 'e', $pos);
        $num = substr($data, $pos, $end - $pos);
        $pos = $end + 1;
        return intval($num);
    } elseif ($data[$pos] == 'd') {
        $pos++;
        $out = array();
        while ($data[$pos] != 'e') {
            $key = bdecode($data, $pos);
            $val = bdecode($data, $pos);
            $out[$key] = $val;
        }
        $pos++;
        return $out;
    } elseif ($data[$pos] == 'l') {
        $pos++;
        $out = array();
        while ($data[$pos] != 'e') {
            $out[] = bdecode($data, $pos);
        }
        $pos++;
        return $out;
    } else {
        $end = strpos($data, ':', $pos);
        $num = substr($data, $pos, $end - $pos);
        $pos = $end + 1 + intval($num);
        return substr($data, $end + 1, intval($num));
    }
}

// Bencode encoding function
function bencode($data) {
    if (is_int($data)) {
        return 'i' . $data . 'e';
    } elseif (is_string($data)) {
        return strlen($data) . ':' . $data;
    } elseif (is_array($data)) {
        if (array_keys($data) === range(0, count($data) - 1)) {
            $out = 'l';
            foreach ($data as $val) {
                $out .= bencode($val);
            }
            $out .= 'e';
        } else {
            $out = 'd';
            ksort($data, SORT_STRING);
            foreach ($data as $key => $val) {
                $out .= strlen($key) . ':' . $key . bencode($val);
            }
            $out .= 'e';
        }
        return $out;
    }
}

function getCurl($url){
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)',
        'Accept: */*',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        'Connection: keep-alive'
    ),
  ));
  $response = curl_exec($curl);
  curl_close($curl);
  ob_flush();
  flush();
  return $response;
}
?>